package com.hellowx.manager.controller;

import com.hellowx.common.utils.FtpUtils;
import com.hellowx.manager.service.UploadService;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 *  文件上传controller
 * @author MagicMushroom
 * @date 2021/7/14
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Resource
    private UploadService uploadService;

    /**
     * 上传图片
     * @param file 图片文件对象
     * @return 是否采成功
     */
    @RequestMapping("/image")
    public String upload(@Param("file") MultipartFile file) throws IOException {

//        基础路径
        String basePath="/images/category/";
//        存储的名称
        String name="goodsCategory_"+System.currentTimeMillis()+".png";
        try {
            uploadService.uploadImage((FileInputStream)file.getInputStream(),basePath+name);
//            FtpUtils.uploadImage((FileInputStream) file.getInputStream(),basePath+name);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return FtpUtils.BASE_IMG_PATH+basePath+name;
    }
}
